package com.pdfbox.sample.quickstart;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;

public class _12AddJavaScript {

  public static void main (String args[]) throws IOException {
    String existFileName = "E:/PdfBox_Examples/周报_add_multiple_text.pdf";
    String exportFileName = "E:/PdfBox_Examples/周报_add_java_script.pdf";

    // 打开文档后会弹出 JS 消息框

    // 第1步:创建一个空文档
    File file = new File(existFileName);
//    PDDocument document = PDDocument.load(file);
    PDDocument document = Loader.loadPDF(file);
    String javaScript = "app.alert({cMsg: 'this is an example', nIcon: 3, nType: 0, cTitle: 'PDFBox Javascript example'});";

    // 第2步:创建PDActionJavaScript对象
    PDActionJavaScript pDAjavascript = new PDActionJavaScript(javaScript);

    // 第3步:在文档中嵌入JavaScript脚本
    document.getDocumentCatalog().setOpenAction(pDAjavascript);

    // 第4步:保存文档
    document.save( new File(exportFileName) );

    // 第5步:关闭文档
    document.close();

    System.out.println("export " + exportFileName + " success");
  }

}
